package com.tom.architect02.ui10.day07;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;
import com.tom.architect02.R;

/**
 *
 */
public class LetterSideBar extends View {
    private Paint mPaint; // 可以优化为多个Paint
    private static String[] mLetters = {
            "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
            "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "#"};
    private String mCurrentTouchLetter; // 当前点击的字母

    private int mTextColor = Color.BLUE;
    private int mFocusColor =Color.RED;
    private float mTextSize = sp2px(12);

    public LetterSideBar(Context context) {
        this(context, null);
    }
    public LetterSideBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public LetterSideBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // 自定义属性
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.LetterSideBar);
        mTextSize = array.getDimensionPixelSize(R.styleable.LetterSideBar_letterTextSize, (int) mTextSize);
        mTextColor = array.getColor(R.styleable.LetterSideBar_letterTextColor, mTextColor);
        mFocusColor = array.getColor(R.styleable.LetterSideBar_letterFocusColor, mFocusColor);
        array.recycle();

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setTextSize(mTextSize); // 设置像素：sp转像素
        mPaint.setColor(mTextColor);
    }

    private float sp2px(int sp) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
                sp, getResources().getDisplayMetrics());
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int textWidth = (int) mPaint.measureText("A"); // 获取字母的宽度
        // 宽度 = 左右padding + 字母宽度
        int width = getPaddingLeft() + getPaddingRight() + textWidth;
        int height = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // 绘制26个字母，#号。
        int itemHeight = (getHeight() - getPaddingTop() - getPaddingBottom()) / mLetters.length;
        for (int i = 0; i < mLetters.length; i++) {
            // 基线是基于中线位置
            int letterCenterY = getPaddingTop() + i * itemHeight + itemHeight / 2;
            // 求基线 baseLineY
            Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
            int dy = (int) ((fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom);
            int baseLineY = letterCenterY + dy;
            int textWidth = (int) mPaint.measureText(mLetters[i]);
            int x = getWidth() / 2 - textWidth / 2;

            // 字母高亮
            if (mLetters[i].equals(mCurrentTouchLetter)) {
                mPaint.setColor(mFocusColor);
                mPaint.setTypeface(Typeface.DEFAULT_BOLD);
                mPaint.setTextSize(mTextSize);
                canvas.drawText(mLetters[i], x, baseLineY, mPaint);
            } else { // 普通字母
                mPaint.setColor(mTextColor);
                mPaint.setTypeface(Typeface.DEFAULT);
                mPaint.setTextSize(mTextSize-10);
                canvas.drawText(mLetters[i], x, baseLineY, mPaint);
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
//            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                // 获取当前位置，拿到字母。
                float currentMoveY = event.getY();
                int itemHeight = (getHeight() - getPaddingTop() - getPaddingBottom()) / mLetters.length;
                int currentPosition = (int) (currentMoveY / itemHeight);

                if (currentPosition < 0) currentPosition = 0;
                if (currentPosition >= mLetters.length) currentPosition = mLetters.length - 1;
                String letter =  mLetters[currentPosition];
                // 优化，避免过度重绘。
                if (letter.equals(mCurrentTouchLetter)) return true;

                mCurrentTouchLetter = letter;
                if (mListener != null) {
                    mListener.touch(mCurrentTouchLetter, true);
                }
                invalidate(); // 重新绘制
                break;
            case MotionEvent.ACTION_UP:
                if (mListener != null) {
                    // 添加延时消失
                    mListener.touch(mCurrentTouchLetter, false);
                }
                mCurrentTouchLetter = null;
                invalidate();
                break;
        }
        return true;
    }

    private LetterTouchListener mListener;
    public void setOnLetterTouchListener(LetterTouchListener listener) {
        this.mListener = listener;
    }
    public interface LetterTouchListener {
        void touch(CharSequence letter, boolean isTouch);
    }

}
